/**
*	bootstrap button 定义公共js模块
*   @Author jackson wang
*   @date 2016-09-022
*   @version 1.0
*
*/

define(["jquery",'jqueryValidate','bsMsgServ','init'], 
	function($,jqueryValidate,bsMsgServ,init) {
	
	
	
	var btnConfig = {
		listAdd:'<button type="button" class="btn btn-warning btn-sm pull-right" btn-type="listAdd">'+
				'<span class="glyphicon glyphicon-plus"></span> 新增'+	           
			'</button>',
		listAddReplace:'<button type="button" class="btn btn-warning btn-sm pull-right" btn-type="listAddReplace">'+
				'<span class="glyphicon glyphicon-plus"></span> 新增'+	           
			'</button>',
		listEdit:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listEdit">'+
				'<span class="glyphicon glyphicon-edit"></span> 编辑'+	           
			'</button>',
		listEditReplace:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listEditReplace">'+
				'<span class="glyphicon glyphicon-edit"></span> 编辑'+	           
			'</button>',
		listViewReplace:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listViewReplace">'+
				'<span class="glyphicon glyphicon-eye"></span> 查看'+	           
			'</button>',
		listDel:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listDel">'+
				'<span class="glyphicon glyphicon-remove"></span> 删除'+	           
			'</button>',
		listRealDel:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listRealDel">'+
				'<span class="glyphicon glyphicon-remove"></span> 彻底删除'+	           
			'</button>',	
			
		listSubmit:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listSubmit">'+
				'<span class="glyphicon glyphicon-submit"></span> 提交审核'+	           
			'</button>',
		listPermit:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listPermit">'+
				'<span class="glyphicon glyphicon-archive"></span> 审核通过'+	           
			'</button>',
		listDenied:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listDenied">'+
				'<span class="glyphicon glyphicon-denied"></span> 审核未通过'+	           
			'</button>',
		editSave:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editSave">'+
				'<span class="glyphicon glyphicon-save"></span> 保存'+	           
			'</button>',
		editSaveReplace:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editSaveReplace">'+
				'<span class="glyphicon glyphicon-save"></span> 保存'+	           
			'</button>',
		editCancel:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editCancel">'+
				'<span class="glyphicon glyphicon-cancel"></span> 取消'+	           
			'</button>',
		editCancelReplace:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editCancel">'+
				'<span class="glyphicon glyphicon-cancel"></span> 取消'+	           
			'</button>',
		editCancelReplace:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editCancelReplace">'+
				'<span class="glyphicon glyphicon-cancel"></span> 取消'+	           
			'</button>',	
			
		editPreview:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editPreview">'+
					'<span class="glyphicon glyphicon-preview"></span> 预览'+	           
				'</button>',
		editFile:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editFile">'+
				'<span class="glyphicon glyphicon-archive"></span> 附件'+	           
			'</button>',
		editRedo:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editRedo">'+
				'<span class="glyphicon glyphicon-send"></span> 发送'+	           
			'</button>',
		editRedoReplace:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editRedoReplace">'+
				'<span class="glyphicon glyphicon-send"></span> 发送'+	           
			'</button>',
		editUndo:'<button type="button" class="btn btn-danger btn-sm pull-right" btn-type="editUndo">'+
				'<span class="glyphicon glyphicon-back"></span> 回退'+	           
			'</button>',
		editUndoReplace:'<button type="button" class="btn btn-danger btn-sm pull-right" btn-type="editUndoReplace">'+
				'<span class="glyphicon glyphicon-back"></span> 回退'+	           
			'</button>',
		editFlowInfo:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editFlowInfo">'+
				'<span class="glyphicon glyphicon-node"></span> 流程信息'+	           
			'</button>',
		editFlowDlg:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editFlowDlg">'+
				'<span class="glyphicon glyphicon-graph"></span> 流程图'+	           
			'</button>',
		editSubmit:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editSubmit">'+
				'<span class="glyphicon glyphicon-submit"></span> 提交审核'+	           
			'</button>',
		editPermit:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editPermit">'+
				'<span class="glyphicon glyphicon-archive"></span> 审核通过'+	           
			'</button>',
		editDenied:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editDenied">'+
				'<span class="glyphicon glyphicon-denied"></span> 审核未通过'+	           
			'</button>',
		listView:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listView">'+
				'<span class="glyphicon glyphicon-preview"></span> 查看'+	           
			'</button>',
		exportExcel:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="exportExcel">'+
				'<span class="glyphicon glyphicon-export"></span> 导出'+	           
			'</button>',
		editExportWord:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editExportWord">'+
				'<span class="glyphicon glyphicon-export"></span> 导出表单'+	           
			'</button>',
		exFinished:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="exFinished">'+
				'<span class="glyphicon glyphicon-export"></span> 导出'+	           
			'</button>',
		writeMail	: '<button type="button" class="btn btn-info btn-sm pull-right" btn-type="writeMail">' +
				'<span class="glyphicon glyphicon-plus"></span>写信</button>',
		reWrite	: '<button type="button" class="btn btn-info btn-sm pull-right" btn-type="reWrite">' +
				'<span class="glyphicon glyphicon-plus"></span>转发</button>',
		reply	: '<button type="button" class="btn btn-info btn-sm pull-right" btn-type="reply">' +
				'<span class="glyphicon glyphicon-backward"></span>回复</button>',
		editDraft:'<button type="button" class="btn btn-info btn-sm pull-right" btn-type="editDraft">'+
				'<span class="glyphicon glyphicon-save"></span> 存草稿'+	           
			'</button>'
			
				
	}
	
	
	//列表按钮缓存信息
	var listBtnConfig = {};
	
	
	//列表查询统计按钮缓存信息
	var listVewBtnConfig ={};
	//编辑按钮缓存信息
	var editBtnConfig = {};
	
	//查看按钮缓存信息
	var viewBtnConfig = {};
	
	var public_method = {
		
		/**
		 * 获取按钮html
		 * @param _options.bizcode	业务编码
		 * @param _options.bizType	业务类型(0:flow,1:form,2:audit)
		 * @param _options.btnType	按钮类型(0:list,1:edit,2:view,3:listView)
		 */
		getBtn:function(_options){
			var ret = private_method.getBtn(_options);
			return ret;
		}
		,
		//获取列表按钮
		getListViewBtn:function(){
			var _btn = '<button type="button" class="btn btn-info btn-sm pull-right" btn-type="listEditReplace">' +
					'<span class="glyphicon glyphicon-edit"></span> 查看</button>'
			return _btn;
		}
		
	}
	
	var private_method = {
		
		/**
		 * 获取列表按钮
		 * @param _options.bizcode	业务编码
		 * @param _options.btnType	按钮类型
		 */
		getCacheBtn:function(_options){
			var _df = {
				btnType:'list'
			}
			var ret
			var _default = $.extend({},_df,_options)
			if(_default && _default.bizcode && _default.btnType){
				
				switch(_default.btnType){
					case 'list':
						ret= listBtnConfig[_options.bizcode];
						break;
					case 'edit':
						ret = editBtnConfig[_options.bizcode];
						break;
					case 'view':
						ret = viewBtnConfig[_options.bizcode];
						break;
					case 'listView':
						ret = listVewBtnConfig[_options.bizcode];
						break;	
				}				
			}
			
			return ret;
		}
		,
		/**
		 * 获取列表按钮
		 */
		getBtn:function(_options){
			
			var _html = private_method.getCacheBtn(_options);
			
			if(!_html){
				var _param = {
					url : '/'+init.getSite()+'/permission/query/getBsUserBizBtn.action',
					data:_options,
					type:'post',
					async:false,
					success:function(data){
						if(data){
							var listHtml='',editHtml='',viewHtml='',listViewHtml='';
								
							$.each(data,function(i,o){
								if(o && o.bizcode && o.type=='O'){
									
									//判断按钮定义是否存在
									if(btnConfig[o.bizcode ]){
										//列表页面按钮
										if(o.listUsed){
											listHtml = btnConfig[o.bizcode] + listHtml;
										}
										//编辑页面按钮
										if(o.editUsed){
											editHtml = btnConfig[o.bizcode] +editHtml;
										}
										//查看页面按钮
										if(o.viewUsed){
											viewHtml = btnConfig[o.bizcode] +viewHtml;
										}
										if(o.listViewUsed){
											listViewHtml = btnConfig[o.bizcode] +listViewHtml;
										}
									}else{
										if(window.console){
											console.log(btnConfig[o.bizcode ] + ' button not found...')
										}
									}									
									
								}
							})
							
							//默认查询统计页面的查看按钮									
							listViewHtml += btnConfig.listViewReplace;
							
							if(!viewHtml || !viewHtml.indexOf('editCancel')){
								viewHtml +=btnConfig.editCancelReplace;
							}
								
							listBtnConfig[_options.bizcode] = listHtml;
							
							//编辑按钮缓存信息
							editBtnConfig[_options.bizcode] = editHtml;							
							
							//查看按钮缓存信息
							viewBtnConfig[_options.bizcode] = viewHtml;
							
							listVewBtnConfig[_options.bizcode] = listViewHtml;
							
							_html = private_method.getCacheBtn(_options);
						}
					}
					,
					failure:function(){
						bsMsgServ.errorSlide('获取操作权限失败，请联系系统管理员！');						
					}
				}
				
				$.ajax(_param);				
			}
			
			return _html;
			
			//查询当前模块列表按钮
			
			//生成当前模块列表按钮
			//var _html = btnConfig.add + btnConfig.edit + btnConfig.del;
			//return _html;
		}
		,
		/**
		 * 获取编辑页面按钮
		 */
		initBtn:function(_options){
			var _url = '';
			
			//查询当前模块列表按钮
			
			//生成当前模块列表按钮
			var _html = btnConfig.save + btnConfig.send + btnConfig.back + btnConfig.archive + btnConfig.nodeList + btnConfig.graph
			return _html;
		}
		
		
	}
	
	return public_method;
})